import { Game } from 'phaser'
import { SceneManager } from './SceneManager'
import { ResourceLoader } from './ResourceLoader'
import { GameState } from '../stores/gameStore'

export class GameManager {
  private game: Game | null = null
  private sceneManager: SceneManager
  private resourceLoader: ResourceLoader
  private gameState: GameState
  private isInitialized: boolean = false

  constructor() {
    this.sceneManager = new SceneManager()
    this.resourceLoader = new ResourceLoader()
    this.gameState = {
      isPlaying: false,
      currentScene: 'menu',
      playerHealth: 100,
      playerLevel: 1,
      currentFloor: 1,
      score: 0,
    }
  }

  /**
   * 初始化游戏引擎
   */
  async init(): Promise<void> {
    if (this.isInitialized) {
      console.warn('GameManager already initialized')
      return
    }

    try {
      // 初始化资源加载器
      await this.resourceLoader.init()

      // 初始化场景管理器
      this.sceneManager.init()

      console.log('GameManager initialized successfully')
      this.isInitialized = true
    } catch (error) {
      console.error('Failed to initialize GameManager:', error)
      throw error
    }
  }

  /**
   * 启动游戏
   */
  async start(): Promise<void> {
    if (!this.isInitialized) {
      throw new Error('GameManager must be initialized before starting')
    }

    try {
      // 创建Phaser游戏实例
      this.game = new Game({
        type: Phaser.AUTO,
        width: 800,
        height: 600,
        parent: 'game-container',
        backgroundColor: '#1e3c72',
        physics: {
          default: 'arcade',
          arcade: {
            gravity: { y: 0 },
            debug: false,
          },
        },
        scene: this.sceneManager.getScenes(),
      })

      // 初始化场景管理器
      this.sceneManager.init(this.game)

      console.log('Game started successfully')
      this.gameState.isPlaying = true
    } catch (error) {
      console.error('Failed to start game:', error)
      throw error
    }
  }

  /**
   * 暂停游戏
   */
  pause(): void {
    if (this.game && this.gameState.isPlaying) {
      this.game.scene.pause()
      this.gameState.isPlaying = false
      console.log('Game paused')
    }
  }

  /**
   * 恢复游戏
   */
  resume(): void {
    if (this.game && !this.gameState.isPlaying) {
      this.game.scene.resume()
      this.gameState.isPlaying = true
      console.log('Game resumed')
    }
  }

  /**
   * 停止游戏
   */
  stop(): void {
    if (this.game) {
      this.game.destroy(true)
      this.game = null
      this.gameState.isPlaying = false
      console.log('Game stopped')
    }
  }

  /**
   * 切换场景
   */
  switchScene(sceneName: string): void {
    if (this.game) {
      this.game.scene.start(sceneName)
      this.gameState.currentScene = sceneName
      console.log('Switched to scene:', sceneName)
    }
  }

  /**
   * 获取游戏状态
   */
  getGameState(): GameState {
    return { ...this.gameState }
  }

  /**
   * 更新游戏状态
   */
  updateGameState(newState: Partial<GameState>): void {
    this.gameState = { ...this.gameState, ...newState }
  }

  /**
   * 获取游戏实例
   */
  getGame(): Game | null {
    return this.game
  }
}
